package com.jsle.financial.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jsle.common.dao.mybatis.BaseDao;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.UserGroup;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.PageAndParameters;
import com.jsle.financial.vo.Result;
import com.jsle.financial.vo.UserAndUserGroup;


public class UsergroupDaoImpl extends BaseDao implements UsergroupDao{
	private Log log = LogFactory.getLog(UsergroupDaoImpl.class);
	/**
	 * 新增一用户组
	 * @param user
	 * @return
	 */
	public Result addUserGroup(UserGroup usergroup){
		Result result = new Result();
		try {
			boolean ret = insert("UserGroup.insertUserGroup", usergroup);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("new usergroup error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 删除一用户组
	 * @param user
	 * @return
	 */
	public Result delUserGroup(UserGroup usergroup){
		Result result = new Result();
		try {
			boolean ret = delete("UserGroup.delUserGroup",usergroup);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			log.error("del usergroup error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 更新一用户组信息
	 * @param user
	 * @return
	 */
	public Result updateUserGroup(UserGroup usergroup){
		Result result = new Result();
		try {
			boolean ret = update("UserGroup.updateUserGroup",usergroup);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_UPDATE_DAO_ERROR_MESSAGE, "User"));
			log.error("update usergroup error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 获取一用户组信息
	 * @param loginName
	 * @return
	 */
	public Result selUserGroup(UserGroup usergroupParameter){
		Result result = new Result();
		try {
			UserGroup usergroup = (UserGroup)queryCountForObject("UserGroup.selUserGroup",usergroupParameter);
			if(usergroup!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(usergroup);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UserGroup"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UserGroup"));
			log.error("query UserGroup error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 分页查询
	 * @param page
	 * @return
	 */
	public Result queryUserGroupsByPage(Page page,String columnName,String paramName){
		Result result = new Result();
		PageAndParameters param = new PageAndParameters(page.getPageIndex(),page.getPageSize(),paramName);
		try {
			List<UserGroup> ugs = new ArrayList<UserGroup>();
			UserGroup ug = new UserGroup();
			int totalCount = 0;
			if(columnName.equals("groupName")){
				ugs = queryForList("UserGroup.queryUserGroupsByGroupName", param);
				ug.setGroupName(paramName);
				totalCount = (Integer)queryCountForObject("UserGroup.queryUserGroupsByGroupNameCounts", ug);
			}else{
				ugs = queryForList("UserGroup.queryUserGroupsByCompanyCode", param);
				ug.setCompanyCode(paramName);
				totalCount = (Integer)queryCountForObject("UserGroup.queryUserGroupsByCompanyCodeCounts", ug);
			}
			
			
			
			if (totalCount < 1){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page());
				return result;
			}
				

			int startIndex = Page.getStartOfPage(page.getPageIndex(), page.getPageSize());
			
			if(ugs!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(new Page(startIndex, totalCount, page.getPageSize(), ugs));
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UserGroupsByPage"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "UserGroupsByPage"));
			log.error("query UserGroups by page error## "+ e.getMessage());
		}
		return result;
	}
	
	/**
	 * 删除用于组与用户关联
	 * @param usergroup
	 * @return
	 */
	public Result delUserUserGroups(UserGroup ug){
		Result result = new Result();
		try {
			boolean ret = delete("UserGroup.delUserUserGroups",ug);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_DEL_DAO_ERROR_MESSAGE, "User"));
			log.error("del usergroup error## "+ e.getMessage());
		}
		return result;
	}
	/**
	 * 根据用户Id获取用户及用户组信息
	 * @param user
	 * @author wisdying
	 * @return
	 */
	public Result queryUserAndUserGroupByUser(User user){
		Result result = new Result();
		try {
			UserAndUserGroup userAndUserGroup = (UserAndUserGroup)queryCountForObject("UserUserGroup.selUserUserGroup",user);
			if(userAndUserGroup!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(userAndUserGroup);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_QUERY_DAO_ERROR_MESSAGE, "User"));
			log.error("queryUserAndUserGroupByUser error## "+ e.getMessage());
		}
		return result;
	}
	public Result queryUserGroupByUserGroupName(UserGroup usergroupParameter) {
		// TODO Auto-generated method stub
		return null;
	}
	public List<UserGroup> selUserGroupsByPage(Page page) {
		// TODO Auto-generated method stub
		return null;
	}
}
